package com.erp.module.yiJianXiang

import android.app.Activity
import androidx.appcompat.app.AlertDialog
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.AdapterView
import com.erp.R
import com.erp.bean.TouSuBean
import com.erp.bean.TouSuSouSuoBean
import com.erp.module.yiJianXiang.sSYiJianXiang.YiJianXiangSouSuoActivity
import com.erp.module.yiJianXiang.xJYiJianXiang.XJYiJianXiangActivity
import com.erp.myAdapter.TouSuAdapter
import com.erp.myFragment.BaseKFragment
import com.erp.util.Myutil
import com.erp.view.PullToRefreshView
import kotlinx.android.synthetic.main.fragment_tousu.*
import java.util.*

/**
 * Created by Administrator on 2018/2/27/027.
 * 作者：刘洋
 * 投诉建议
 */

class FragmentYiJianXIang : BaseKFragment(),YiJianXiangContract.YiJianXiangView, PullToRefreshView.OnFooterRefreshListener, PullToRefreshView.OnHeaderRefreshListener, AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener, View.OnClickListener {


    var updatedialog: AlertDialog? = null
    lateinit var mlist: MutableList<TouSuBean>
    lateinit var adapter: TouSuAdapter
    lateinit var touSuSouSuoBean: TouSuSouSuoBean
    lateinit var P : IYiJianXiangP
    private val f = "FragmentTouSu"
    override fun getLayoutId(): Int {
        fragmentname = f
        return R.layout.fragment_tousu
    }

    override fun initData() {
        touSuSouSuoBean = TouSuSouSuoBean()
        mlist = ArrayList()
        P = IYiJianXiangP(this,context!!,mlist)
        Pull.setheader(true)
        Pull.setOnFooterRefreshListener(this)
        Pull.setOnHeaderRefreshListener(this)
        LvTousu.onItemClickListener = this
        LvTousu.onItemLongClickListener = this
        TvQuery.setOnClickListener(this)
        LlQingKong.setOnClickListener(this)
        LlXingJian.setOnClickListener(this)
        adapter = TouSuAdapter(mlist, context!!)
        LvTousu.adapter = adapter
        P.gainData("YiJianXiang")
        Pull.headerRefreshing()
    }
    fun qingkong(){
        if(isFirstLoad){
            return
        }
        Pull.headerRefreshing()
    }

    fun xingjian() {
        val intent = Intent(activity, XJYiJianXiangActivity::class.java)
        intent.putExtra("code", 1)
        startActivityForResult(intent, 3)
    }

    override fun onItemClick(parent: AdapterView<*>, view: View, position: Int, id: Long) {
        touSuBean = mlist[position]
        val intent = Intent(activity, XJYiJianXiangActivity::class.java)
        intent.putExtra("code", 2)
        startActivityForResult(intent, 3)
    }

    override fun onItemLongClick(parent: AdapterView<*>, view: View, position: Int, id: Long): Boolean {
        var c= context
        if(c != null){
            val dialog = AlertDialog.Builder(c)
            dialog.setTitle("提示")
            dialog.setMessage("确定要删除这条投诉吗?")
            dialog.setPositiveButton("确定") { _, _ ->
                updatedialog = Myutil.showupdatedialog(activity, "正在删除", false)
                P.delete(position)
            }
            dialog.setNegativeButton("取消") { dialog, _ -> dialog.dismiss() }
            dialog.show()
        }

        return true
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(data==null){
            return
        }
        if (resultCode == Activity.RESULT_OK) {
            when (requestCode) {
                3 -> {
                    val isshuxing = data.getBooleanExtra("isxing", false)
                    if (isshuxing) {
                        updatedialog = Myutil.showupdatedialog(activity, "正在刷新", false)
                        P.sousuo(touSuSouSuoBean)
                    } else {
                        Log.d("TAAAG", "bean=" + touSuBean.toString())
                        adapter.notifyDataSetChanged()
                    }
                }
                101 -> {
                    touSuSouSuoBean = TouSuSouSuoBean()
                    val bundle = data.getParcelableExtra<Bundle>("searchParams")
                    touSuSouSuoBean = bundle.getParcelable("searchParams")!!
                    val sb = StringBuffer("")
                    if (touSuSouSuoBean.gongzuodi != "") {
                        sb.append("工作地：").append(touSuSouSuoBean.gongzuodi)
                    }
                    if (touSuSouSuoBean.changqu != "") {
                        sb.append("厂区：").append(touSuSouSuoBean.changqu)
                    }
                    if (touSuSouSuoBean.bumen != "") {
                        sb.append("部门：").append(touSuSouSuoBean.bumen)
                    }
                    if (touSuSouSuoBean.tousu != "") {
                        sb.append("投诉：").append(touSuSouSuoBean.tousu)
                    }
                    if (touSuSouSuoBean.jianyi != "") {
                        sb.append("建议：").append(touSuSouSuoBean.jianyi)
                    }
                    if (touSuSouSuoBean.starttime != "" || touSuSouSuoBean.endtime != "") {
                        sb.append("创建时间：").append("开始时间：").append(touSuSouSuoBean.starttime).append("结束时间:").append(touSuSouSuoBean.endtime)
                    }
                    TvQuery.text = sb.toString()
                    updatedialog = Myutil.showupdatedialog(activity, "正在过滤", false)
                    P.sousuo(touSuSouSuoBean)
                }
            }
        }
    }

   override fun onClick(v: View) {
        when (v) {
            TvQuery -> {
                val intent = Intent(activity, YiJianXiangSouSuoActivity::class.java)
                val bundle = Bundle()
                bundle.putParcelable("searchParams", touSuSouSuoBean)
                intent.putExtra("searchParams", bundle)
                startActivityForResult(intent, 101)
                activity!!.overridePendingTransition(R.anim.start_activity1, R.anim.no_activity)
            }
            LlQingKong -> {
                touSuSouSuoBean = TouSuSouSuoBean()
                TvQuery.text = ""
                updatedialog = Myutil.showupdatedialog(activity, "正在刷新", false)
                P.sousuo(touSuSouSuoBean)
            }
            LlXingJian -> {
                xingjian()
            }
        }
    }

    override fun xialaOk() {
        updatedialog?.dismiss()
        Pull.onHeaderRefreshComplete()
        adapter.notifyDataSetChanged()
    }

    override fun xialaNo() {
        updatedialog?.dismiss()
        Pull.onHeaderRefreshComplete()
    }

    override fun shanglaOk() {
        updatedialog?.dismiss()
        Pull.onFooterRefreshComplete()
        adapter.notifyDataSetChanged()
    }

    override fun shanglaNo() {
        updatedialog?.dismiss()
        Pull.onFooterRefreshComplete()
    }
    override fun gainDataOk() {
        adapter.notifyDataSetChanged()
    }

    override fun onFooterRefresh(view: PullToRefreshView) {
        P.shangla()
    }
    override fun onHeaderRefresh(view: PullToRefreshView) {
        P.xiala()
    }

    override fun deleteOk() {
        updatedialog?.dismiss()
        adapter.notifyDataSetChanged()
    }

    override fun deleteNo() {
        updatedialog?.dismiss()
    }

    companion object {
        var touSuBean: TouSuBean= TouSuBean()
    }
}
